﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace 药品信息管理
{
    public partial class YP_ZDWH : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        string currentLX;//药品类型
        string currentJB;//药品级别
        string currentFLAG;//使用标识
        string cmd_ZSG = "BEGIN TRANSACTION ";


        public YP_ZDWH()
        {
            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is ComboBox) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void data_view()
        {
            dataGridViewX1.Columns[3].Visible = false;
            dataGridViewX1.Columns[4].Visible = false;
            dataGridViewX1.Columns[5].Visible = false;
            dataGridViewX1.Columns[6].Visible = false;
            dataGridViewX1.Columns[7].Visible = false;
            dataGridViewX1.Columns[8].Visible = false;
            dataGridViewX1.Columns[9].Visible = false;
            dataGridViewX1.Columns[10].Visible = false;
            dataGridViewX1.Columns[11].Visible = false;
        }

        #region 显示药品
        private void YP_show()
        {
           
            string cmd_show = "SELECT BM 编码,MC 名称,GG 规格,LX,DH,BZDW,BZDJ ,YBBZ,JB,JX,YLDW,DJ,FLAG FROM ZD_YP WHERE 1=1" + currentLX + currentJB + currentFLAG;
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_yp = new DataTable();
                SqlCommand sc = new SqlCommand(cmd_show, con);
                da.SelectCommand = sc;
                da.Fill(dt_yp);
                dataGridViewX1.DataSource = dt_yp;
                SUMYP.Text = "药品数量：" + dt_yp.Rows.Count.ToString();
                data_view();
            }
        }
        #endregion

        private void YP_ZDWH_Load(object sender, EventArgs e)
        {
            currentLX="";
            currentJB="";
            currentFLAG = "";
            try
            {
                #region 药品级别、剂型初始化
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_jb = new DataTable();
                    DataTable dt_jx = new DataTable();
                    SqlCommand JB = new SqlCommand("SELECT * FROM ZD_YPJB", con);//sql语句
                    SqlCommand JX = new SqlCommand("SELECT * FROM ZD_YPJX", con);
                    da.SelectCommand = JB;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_jb);
                    da.SelectCommand = JX;
                    da.Fill(dt_jx);
                    for (int i = 0; i < dt_jb.Rows.Count; i++)
                    {
                        DevComponents.AdvTree.Node node = new DevComponents.AdvTree.Node();
                        node.Text = dt_jb.Rows[i][1].ToString();
                        node1.Nodes.Add(node);

                        cmb_JB.Items.Add(dt_jb.Rows[i][1].ToString());
                    }
                    for (int i = 0; i < dt_jx.Rows.Count; i++)
                    {
                        cmb_JX.Items.Add(dt_jx.Rows[i][1].ToString());
                    }
                }
                #endregion
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        #region 增加
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            txt_BM.Text = "";
            txt_PY.Text = "";
            txt_MC.Text = "";
            txt_BZDW.Text = "";
            txt_bzdj.Text = "";
            txt_SL.Text = "";
            txt_GG.Text = "";
            txt_DW.Text = "";
            txt_DJ.Text = "";

        }
        #endregion

        #region 删除
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            try
            {
                cmd_ZSG = cmd_ZSG + " DELETE FROM ZD_YP WHERE BM='" + dataGridViewX1.CurrentRow.Cells[0].Value.ToString() + "'";

                dataGridViewX1.Rows.Remove(dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex]);

                timer1.Enabled = true;

                label_del.Text = " 删除成功，请点【保存】确认";

                label_del.Visible = true;
            }
            catch { }
        }
        #endregion

        #region 保存
        private void buttonItem3_Click(object sender, EventArgs e)
        {
            cmd_ZSG = cmd_ZSG + " COMMIT TRANSACTION ";

            try
            {
                SqlConnection cn = new SqlConnection(SQL.connectDB);

                {
                    cn.Open();

                    SqlCommand com = new SqlCommand(cmd_ZSG, cn);

                    com.ExecuteNonQuery();

                    cmd_ZSG = "BEGIN TRANSACTION ";

                }
            }
            catch { }
            MessageBox.Show("保存成功", "提示");
        }
        #endregion

      
        #region 显示西药
        private void XY_Click(object sender, EventArgs e)
        {
            currentLX = "AND LX=0";
            YP_show();
            //YP_show("and LX=0");
            //currentLX=0;
        }
        #endregion

        #region 显示全部药品
        private void superTabItem1_Click(object sender, EventArgs e)
        {
            currentLX = "";
            YP_show();
            //YP_show("");
            //currentLX = -1;
        }
        #endregion

        #region 显示成药
        private void CY_Click(object sender, EventArgs e)
        {
            currentLX = "AND LX=1";
            YP_show();
            //YP_show("and LX=1");
            //currentLX=1;
        }
        #endregion

        #region 显示草药
        private void ZCY_Click(object sender, EventArgs e)
        {
            currentLX = "AND LX=2";
            YP_show();
            //YP_show("and LX=2");
            //currentLX=2;
        }
        #endregion

        #region 编码定位
        private void textBoxItem1_InputTextChanged(object sender)
        {
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_bmdw = new DataTable();
                SqlCommand BMDW = new SqlCommand("SELECT BM 编码,MC 名称,GG 规格,LX,DH,BZDW,BZDJ ,YBBZ,JB,JX,YLDW,DJ,FLAG FROM ZD_YP WHERE BM LIKE'%" + textBoxItem1.Text.ToString() + "%' OR DH LIKE'%" + textBoxItem1.Text.ToString() + "%' OR MC LIKE'%" + textBoxItem1.Text.ToString() + "%'", con);//sql语句
                da.SelectCommand = BMDW;//设置为已实例化SqlDataAdapter的查询命令
                da.Fill(dt_bmdw);
                dataGridViewX1.DataSource = dt_bmdw;

            }

        }
        #endregion

        #region 药品级别筛选
        private void advTree1_NodeClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            currentJB = "AND JB =(select BM from ZD_YPJB where MC='" + advTree1.SelectedNode.ToString() + "')";
            YP_show();

        }
        #endregion

        private void cmb_LX_TextChanged(object sender, EventArgs e)
        {
            if (cmb_LX.Text == "0")
                cmb_LX.SelectedIndex = 0;
            if (cmb_LX.Text == "1")
                cmb_LX.SelectedIndex = 1;
            if (cmb_LX.Text == "2")
                cmb_LX.SelectedIndex = 2;
        }

        private void cmb_FLAG_TextChanged(object sender, EventArgs e)
        {
            if (cmb_FLAG.Text == "0")
                cmb_FLAG.SelectedIndex = 0;
            if (cmb_FLAG.Text == "1")
                cmb_FLAG.SelectedIndex = 1;
        }

        private void dataGridViewX1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                cmb_LX.SelectedIndex = Convert.ToInt16(dataGridViewX1.CurrentRow.Cells[3].Value);
                txt_BM.Text = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
                txt_PY.Text = dataGridViewX1.CurrentRow.Cells[4].Value.ToString();
                txt_MC.Text = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
                txt_BZDW.Text = dataGridViewX1.CurrentRow.Cells[5].Value.ToString();
                txt_bzdj.Text = dataGridViewX1.CurrentRow.Cells[6].Value.ToString();
                txt_SL.Text = dataGridViewX1.CurrentRow.Cells[7].Value.ToString();
                txt_GG.Text = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
                cmb_JB.SelectedIndex = Convert.ToInt16(dataGridViewX1.CurrentRow.Cells[8].Value) - 1;
                cmb_JX.SelectedIndex = Convert.ToInt16(dataGridViewX1.CurrentRow.Cells[9].Value) - 1;
                txt_DW.Text = dataGridViewX1.CurrentRow.Cells[10].Value.ToString();
                txt_DJ.Text = dataGridViewX1.CurrentRow.Cells[11].Value.ToString();
                cmb_FLAG.SelectedIndex=Convert.ToInt16(dataGridViewX1.CurrentRow.Cells[12].Value);
            }
            catch { }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            DataTable yp_bm = new DataTable();
            int insert_flag = 0;

            using(SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                SqlCommand sc = new SqlCommand("SELECT BM FROM ZD_YP", con);
                da.SelectCommand = sc;
                da.Fill(yp_bm);
                for (int i = 0; i < yp_bm.Rows.Count; i++)
                    //BM 编码,MC 名称,GG 规格,[LX],DH,BZDW ,YBBZ,[JB],[JX],YLDW,DJ,FLAG
                    if (yp_bm.Rows[i][0].ToString() == txt_BM.Text.ToString())
                    {
                        cmd_ZSG = cmd_ZSG + " UPDATE ZD_YP SET FLAG=" + cmb_FLAG.SelectedIndex + ",MC='" + txt_MC.Text.ToString() + "',GG='" + txt_GG.Text.ToString() + "',DH='"
                                  + txt_PY.Text.ToString() + "',BZDW='" + txt_BZDW.Text.ToString() +"',BZDJ='"+txt_bzdj.Text.ToString()+ "',YBBZ='" + txt_SL.Text.ToString() + "',YLDW='" + txt_DW.Text.ToString() + "',DJ='"
                                  + txt_DJ.Text.ToString() + "',LX=" + cmb_LX.SelectedIndex + ",JB=" + Convert.ToInt16(cmb_JB.SelectedIndex + 1) + ",JX=" + Convert.ToInt16(cmb_JX.SelectedIndex + 1) + " WHERE BM='" + txt_BM.Text.ToString() + "'";
                        insert_flag = 1;

                        label_tip.Text = "修改成功，请点【保存】确认";

                        label_tip.ForeColor = System.Drawing.Color.Blue;
                    }
                if (insert_flag != 1)
                {
                    cmd_ZSG = cmd_ZSG + " INSERT INTO ZD_YP(BM,MC,GG,LX,DH,BZDW ,YBBZ,JB,JX,YLDW,DJ,FLAG) VALUES('" + txt_BM.Text.ToString() + "','" + txt_MC.Text.ToString() + "','" + txt_GG.Text.ToString() + "',"
                              + cmb_LX.SelectedIndex + ",'" + txt_PY.Text.ToString() + "','" + txt_BZDW.Text.ToString() +"','"+txt_bzdj.Text.ToString()+ "','" + txt_SL.Text.ToString() + "'," + Convert.ToInt16(cmb_JB.SelectedIndex + 1) + "," + Convert.ToInt16(cmb_JX.SelectedIndex + 1)
                              + ",'" + txt_DW.Text.ToString() + "','"+txt_DJ.Text.ToString()+"',"+cmb_FLAG.SelectedIndex+")";

                    label_tip.Text = "增加成功，请点【保存】确认";

                    label_tip.ForeColor = System.Drawing.Color.Green;
                }
            }

            label_tip.Visible = true;
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label_del.Visible = false;

            label_tip.Visible = false;
        }

        private void comboBoxItem1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBoxItem1.SelectedIndex)
            {
                case 0:
                    //不显示停用药品
                    {
                        currentFLAG = "AND FLAG=1";
                    } break;
                case 1:
                    //全部显示
                    {
                        currentFLAG = "";
                    } break;
                case 2:
                    //只显示停用药品
                    {
                        currentFLAG = "AND FLAG=0";
                    } break;

            }
            YP_show();
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
        }



    }
}